﻿#include "pch.h"
#include "MFCApplication2.h"
#include "ColorDialog.h"
#include "afxdialogex.h"
UINT BCGM_CHANGECOLOR = ::RegisterWindowMessage(L"BCGM_CHANGECOLOR");
IMPLEMENT_DYNAMIC(ColorDialog, CBCGPPropertyPage)



ColorDialog::ColorDialog(CWnd* pParent /*=nullptr*/)
	: CBCGPPropertyPage(IDD_DLG_COLOR, IDS_ABOUTBOX){

	m_clrCurrent = RGB(255, 0, 255);
}

ColorDialog::~ColorDialog(){

}

void ColorDialog::DoDataExchange(CDataExchange* pDX){

	CBCGPPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BTN_BTN_IMAGE, m_autoCorlorbtn);
	DDX_Control(pDX, IDC_BTN_SET_DESC, m_wndColorPicker);
	DDX_Control(pDX, IDC_BTN_MENU, m_wndLuminancePicker);
	DDX_Control(pDX, IDC_BTN_TOGGLE, m_wndHuePicker);
	DDX_Control(pDX, IDC_BTN_COL_BRUSH, m_btnBrush);
	DDX_Control(pDX, IDC_CMB_BTN_THEME, m_btnCombox);
	DDX_Control(pDX, IDC_CMB_VISUAL_THEME, m_cmbTheme1);
}

BEGIN_MESSAGE_MAP(ColorDialog, CBCGPPropertyPage)
	ON_BN_CLICKED(IDC_BTN_COLOR, &ColorDialog::OnClickedBtnColor)
	ON_BN_CLICKED(IDC_BTN_BTN_IMAGE, &ColorDialog::OnClickedBtnColorPicker)
	ON_BN_CLICKED(IDC_CHECK_CIRCLE, &ColorDialog::OnClickedBtnColorCircle)
	ON_BN_CLICKED(IDC_BTN_SET_DESC, &ColorDialog::OnClickedBtnColorButton2)
	ON_STN_CLICKED(IDC_COLORBAR2, &ColorDialog::OnClickedBtnColorbar2)
	ON_BN_CLICKED(IDC_BTN_TOGGLE, &ColorDialog::OnClickedBtnColorButton4)
	ON_STN_CLICKED(IDC_STATIC_COLORBAR, &ColorDialog::OnStnClickedStaticColorbar)
	ON_CBN_SELCHANGE(IDC_CMB_VISUAL_THEME, &ColorDialog::OnSelChangeCmbChangeTheme)
END_MESSAGE_MAP()

void ColorDialog::ApplyTheme(const CString& csThemeName){
	/*确保启用视觉管理器样式*/
	EnableVisualManagerStyle(TRUE);

	/*清除当前的视觉管理器*/
	CBCGPVisualManager::DestroyInstance();


	if (csThemeName == L"Windows Native Theme"){

		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManagerXP));
	}
	else if (csThemeName == L"Visual Studio 2012"){

		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManagerVS2012));
	}
	else if (csThemeName == L"Visual Studio 2010"){

		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManagerVS2010));
	}
	else if (csThemeName == L"Visual Studio 2008"){

		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManagerVS2008));
	}
	else if (csThemeName == L"Visual Studio 2005"){

		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManagerVS2005));
	}
	else if (csThemeName == L"MS Office 2013"){

		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManager2013));
	}
	else if (csThemeName == L"MS Office 2010"){

		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManager2010));
	}
	else if (csThemeName == L"MS Office 2007 Blue"){

		CBCGPVisualManager2007::SetStyle(CBCGPVisualManager2007::VS2007_LunaBlue);
		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManager2007));
	}
	else if (csThemeName == L"MS Office 2007 Black"){

		CBCGPVisualManager2007::SetStyle(CBCGPVisualManager2007::VS2007_ObsidianBlack);
		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManager2007));
	}
	else if (csThemeName == L"MS Office 2007 Silver"){

		CBCGPVisualManager2007::SetStyle(CBCGPVisualManager2007::VS2007_Silver);
		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManager2007));
	}
	else if (csThemeName == L"MS Office 2007 Aqua"){

		CBCGPVisualManager2007::SetStyle(CBCGPVisualManager2007::VS2007_Aqua);
		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManager2007));
	}
	else if (csThemeName == L"MS Office 2003"){

		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManager2003));
	}
	else if (csThemeName == L"Carbon"){

		CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManagerCarbon));
	}
	/*刷新整个对话框*/
	RedrawWindow(NULL, NULL, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_UPDATENOW | RDW_FRAME | RDW_ERASE);

	/* 更新所有子控件*/
	CWnd* pChild = GetWindow(GW_CHILD);
	while (pChild){

		pChild->RedrawWindow(NULL, NULL, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
		pChild = pChild->GetWindow(GW_HWNDNEXT);
	}

}
BOOL ColorDialog::OnInitDialog(){

	CBCGPPropertyPage::OnInitDialog();

	EnableVisualManagerStyle(TRUE);

	m_autoCorlorbtn.EnableAutomaticButton(L"Automatic", RGB(255, 255, 255), TRUE);
	m_autoCorlorbtn.EnableOtherButton(L"Other", TRUE, TRUE);
	m_autoCorlorbtn.SetColor(RGB(100, 149, 237));
	m_autoCorlorbtn.SetWebColors(); /* 让色盘变成大色块表*/
	m_autoCorlorbtn.m_nFlatStyle = CBCGPButton::BUTTONSTYLE_FLAT;
	COLORREF clr = m_autoCorlorbtn.GetColor();
	CString csStr;
	csStr.Format(L"%d,%d,%d", GetRValue(clr), GetGValue(clr), GetBValue(clr));
	SetDlgItemText(IDC_STATIC_CURCOLOR, csStr);



	/*color bar*/
	CRect rectColorBar;
	GetDlgItem(IDC_STATIC_COLORBAR)->GetWindowRect(&rectColorBar);
	ScreenToClient(&rectColorBar);
	/*创建颜色选择器*/
	if (!m_colorBar.CreateOnPlaceHolder(this, IDC_STATIC_COLORBAR, 0, WS_CHILD | WS_VISIBLE | WS_TABSTOP, FALSE)){

		TRACE(L"Failed to create color bar!\n");
		return FALSE;
	}
	/*设置颜色选择器的位置和大小*/
	m_colorBar.MoveWindow(rectColorBar);
	/* 设置颜色选择器的属性*/
	m_colorBar.SetVertMargin(0);     
	m_colorBar.SetHorzMargin(0);     
	m_colorBar.EnableOtherButton(L"Other...");
	m_colorBar.EnableBorder(TRUE);  
	m_colorBar.ContextToSize(TRUE, TRUE);  
	COLORREF defaultColor = RGB(0, 0, 0); 
	m_colorBar.SetColor(defaultColor);
	CString csStrRGB;
	csStrRGB.Format(L"%d, %d, %d",
		GetRValue(defaultColor),
		GetGValue(defaultColor),
		GetBValue(defaultColor));
	SetDlgItemText(IDC_STATIC_COLOR_VALUE, csStrRGB);
	m_colorBar.Invalidate();
	m_colorBar.UpdateWindow();


	/*color palette*/
	/*创建系统调色板*/
	CBCGPClientDC dc(this);
	int nColors = 256; 
	UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
	LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
	pLP->palVersion = 0x300;
	pLP->palNumEntries = (USHORT)nColors;
	::GetSystemPaletteEntries(dc.GetSafeHdc(), 0, nColors, pLP->palPalEntry);
	CPalette m_palSys;
	m_palSys.CreatePalette(pLP);
	delete[] pLP;
	m_wndColorPicker.SetType(CBCGPColorPickerCtrl::PICKER);
	m_wndColorPicker.SetPalette(&m_palSys);
	m_wndColorPicker.SetRGB(RGB(255, 0, 0));
	m_wndLuminancePicker.SetType(CBCGPColorPickerCtrl::LUMINANCE);
	m_wndLuminancePicker.SetPalette(&m_palSys);
	m_wndColorPicker.RedrawWindow();
	m_wndLuminancePicker.RedrawWindow();


	/*color hue*/
	m_wndHuePicker.SetType(CBCGPColorPickerCtrl::PICKERH); 
	m_wndHuePicker.SetPalette(&m_palSys);
	m_wndHuePicker.SetRGB(RGB(0,255,0));
	
	/*brush button*/
	/*设置初始画刷（如黄色径向渐变）*/
	CBCGPBrush brush;
	brush.SetColor(CBCGPColor(RGB(255, 200, 80)));
	m_btnBrush.SetBrush(brush);

	/*color combox*/
	m_btnCombox.EnableNoneColor(TRUE, L"No color");
	m_btnCombox.AddColor(RGB(255, 128, 128), L"Important");
	m_btnCombox.AddColor(RGB(255, 192, 128), L"Business");
	m_btnCombox.AddColor(RGB(192, 255, 128), L"Personal");
	m_btnCombox.AddColor(RGB(255, 255, 128), L"Vacation");
	m_btnCombox.AddColor(RGB(255, 255, 0), L"Must Attend");
	m_btnCombox.AddColor(RGB(128, 255, 255), L"Travel Required");
	m_btnCombox.AddColor(RGB(192, 192, 255), L"Birthday");
	m_btnCombox.AddColor(RGB(192, 128, 255), L"Anniversary");
	m_btnCombox.AddColor(RGB(255, 224, 128), L"Phone Call");
	m_btnCombox.SelectColor(RGB(192, 255, 128));
	m_btnCombox.SelectColor(RGB(192, 255, 128));


	/*color bar with palatte*/

	#define NUM_COLOURS 64
	struct{

		LOGPALETTE    LogPalette;
		PALETTEENTRY  PalEntry[NUM_COLOURS];
	}pal;

	LOGPALETTE* pLogPalette = (LOGPALETTE*)&pal;
	pLogPalette->palVersion = 0x300;
	pLogPalette->palNumEntries = (WORD)NUM_COLOURS;

	COLORREF colorStart = RGB(0, 0, 255);
	COLORREF colorFinish = RGB(255, 255, 255);

	int nShift = 6;
	for (int i = 0; i < NUM_COLOURS; i++) {

		BYTE bR = (BYTE)((GetRValue(colorStart) * (NUM_COLOURS - i) +
			GetRValue(colorFinish) * i) >> nShift);
		BYTE bG = (BYTE)((GetGValue(colorStart) * (NUM_COLOURS - i) +
			GetGValue(colorFinish) * i) >> nShift);
		BYTE bB = (BYTE)((GetBValue(colorStart) * (NUM_COLOURS - i) +
			GetBValue(colorFinish) * i) >> nShift);

		pLogPalette->palPalEntry[i].peRed = bR;
		pLogPalette->palPalEntry[i].peGreen = bG;
		pLogPalette->palPalEntry[i].peBlue = bB;
		pLogPalette->palPalEntry[i].peFlags = 0;
	}

	m_palColorPicker.CreatePalette(pLogPalette);
	m_wndColorBar2.SetHorzMargin(0);
	m_wndColorBar2.SetVertMargin(0);
	m_wndColorBar2.EnableBorder();

	m_wndColorBar2.CreateOnPlaceHolder(this, IDC_COLORBAR2, 0, WS_CHILD | WS_VISIBLE | WS_TABSTOP, FALSE, (LPVOID)&m_palColorPicker);
	m_wndColorBar2.SetColor(colorStart);
	


	CBCGPVisualManager::SetDefaultManager(RUNTIME_CLASS(CBCGPVisualManager2007));

	/*初始化主题下拉框*/
	m_cmbTheme1.SubclassDlgItem(IDC_CMB_VISUAL_THEME, this);
	m_cmbTheme1.ResetContent();
	m_cmbTheme1.AddString(L"Windows Native Theme");
	m_cmbTheme1.AddString(L"Visual Studio 2012");
	m_cmbTheme1.AddString(L"Visual Studio 2010");
	m_cmbTheme1.AddString(L"Visual Studio 2008");
	m_cmbTheme1.AddString(L"Visual Studio 2005");
	m_cmbTheme1.AddString(L"MS Office 2013");
	m_cmbTheme1.AddString(L"MS Office 2010");
	m_cmbTheme1.AddString(L"MS Office 2007 Blue");
	m_cmbTheme1.AddString(L"MS Office 2007 Black");
	m_cmbTheme1.AddString(L"MS Office 2007 Silver");
	m_cmbTheme1.AddString(L"MS Office 2007 Aqua");
	m_cmbTheme1.AddString(L"MS Office 2003");
	m_cmbTheme1.AddString(L"Carbon");
	m_cmbTheme1.SetCurSel(0);
	ApplyTheme(L"Windows Native Theme");
	return TRUE;
}
void ColorDialog::OnSelChangeCmbChangeTheme(){

	int nIndex = m_cmbTheme1.GetCurSel();
	if (nIndex != CB_ERR){

		CString csStrTheme;
		m_cmbTheme1.GetLBText(nIndex, csStrTheme);
		ApplyTheme(csStrTheme);
	}
}

void ColorDialog::OnClickedBtnColor(){

	COLORREF clr = m_clrCurrent;
	CBCGPColorDialog dlg(clr, 0, this); 

	if (dlg.DoModal() == IDOK){

		m_clrCurrent = dlg.GetColor();
		CString csStr;
		csStr.Format(L"%d, %d, %d",
			GetRValue(m_clrCurrent),
			GetGValue(m_clrCurrent),
			GetBValue(m_clrCurrent));
		SetDlgItemText(IDC_STATIC_COLOR, csStr);
	}
}
void ColorDialog::OnClickedBtnColorCircle(){

	m_autoCorlorbtn.m_bDrawCircle = (IsDlgButtonChecked(IDC_CHECK_CIRCLE) == BST_CHECKED);
	m_autoCorlorbtn.Invalidate();
}
void ColorDialog::OnClickedBtnColorPicker(){

	COLORREF clr = m_autoCorlorbtn.GetColor();
	CString csStr;
	csStr.Format(L"%d,%d,%d", GetRValue(clr), GetGValue(clr), GetBValue(clr));
	SetDlgItemText(IDC_STATIC_CURCOLOR, csStr);
}
void ColorDialog::OnLButtonUp(UINT nFlags, CPoint point){

	CBCGPPropertyPage::OnLButtonUp(nFlags, point);
	CRect rect;
	m_wndColorPicker.GetWindowRect(&rect);
	ScreenToClient(&rect);

	if (rect.PtInRect(point)){

		double hue, lum, sat;
		m_wndColorPicker.GetHLS(&hue, &lum, &sat);

		m_wndLuminancePicker.SetHue(hue);
		m_wndLuminancePicker.SetSaturation(sat);
		m_wndLuminancePicker.Invalidate();

		COLORREF color = m_wndColorPicker.GetColor();
		CString csStr;
		csStr.Format(L"%d,%d,%d", GetRValue(color), GetGValue(color), GetBValue(color));
		SetDlgItemText(IDC_STATIC, csStr);
	}
}

/*color palatte*/
void ColorDialog::OnClickedBtnColorButton2(){

	COLORREF color = m_wndColorPicker.GetColor();
	m_wndLuminancePicker.SetRGB(color);
	CString csStrRGB;
	csStrRGB.Format(L"%d, %d, %d",
		GetRValue(color),
		GetGValue(color),
		GetBValue(color));

	SetDlgItemText(IDC_STATIC_PALETTE, csStrRGB);
}

void ColorDialog::OnClickedBtnColorbar2(){
	
	COLORREF color = m_wndColorBar2.GetColor();
	CString csStrRGB;
	csStrRGB.Format(L"%d, %d, %d",
		GetRValue(color),
		GetGValue(color),
		GetBValue(color));

	SetDlgItemText(IDC_STARLEXT, csStrRGB);
}


void ColorDialog::OnClickedBtnColorButton4(){

	COLORREF color = m_wndHuePicker.GetColor();
	CString csStrRGB;
	csStrRGB.Format(L"%d, %d, %d",
		GetRValue(color),
		GetGValue(color),
		GetBValue(color));

	SetDlgItemText(IDC_STATIC_HUE, csStrRGB);
}


void ColorDialog::OnStnClickedStaticColorbar(){
	
	COLORREF color = m_colorBar.GetColor();
	CString csStrRGB;
	csStrRGB.Format(L"%d, %d, %d",
		GetRValue(color),
		GetGValue(color),
		GetBValue(color));

	SetDlgItemText(IDC_STATIC_COLOR_VALUE, csStrRGB);
}
